// Fill out your copyright notice in the Description page of Project Settings.


#include "SPakPlatformRow.h"
#include "SlateOptMacros.h"
#include "Widgets/Text/STextBlock.h"
#include "Widgets/Input/SCheckBox.h"
#include "SPakPlatformsView.h"

const FName SPakPlatformRow::Col_PlatformName(TEXT("PlatformName"));

BEGIN_SLATE_FUNCTION_BUILD_OPTIMIZATION
void SPakPlatformRow::Construct(const FArguments& InArgs, SPakPlatformsView* InPlatformsView)
{
	PlatformName = InArgs._PlatformName;
	PlatformsView = InPlatformsView;
	SMultiColumnTableRow<TSharedPtr<FString> >::Construct(FSuperRowType::FArguments(), InArgs._OwnerTableView.ToSharedRef());
}
END_SLATE_FUNCTION_BUILD_OPTIMIZATION

TSharedRef<SWidget> SPakPlatformRow::GenerateWidgetForColumn(const FName& ColumnName)
{
	if (ColumnName.IsEqual(Col_PlatformName))
	{
		return SNew(SCheckBox)
			.IsChecked(this, &SPakPlatformRow::HandleCheckBoxIsChecked)
			.OnCheckStateChanged(this, &SPakPlatformRow::HandleCheckBoxCheckStateChanged)
			.Padding(FMargin(6.0, 2.0))
			[
				SNew(STextBlock)
				.Text(FText::FromString(*PlatformName))
			];
	}
	return SNullWidget::NullWidget;
}

void SPakPlatformRow::HandleCheckBoxCheckStateChanged(ECheckBoxState NewState)
{
	if (NewState == ECheckBoxState::Checked)
		PlatformsView->AddSelectPlatform(*PlatformName);
	else
		PlatformsView->RemoveSelectPlatform(*PlatformName);
}

ECheckBoxState SPakPlatformRow::HandleCheckBoxIsChecked() const
{
	if (PlatformsView->GetAllSelectPlatforms().Contains(*PlatformName))
		return ECheckBoxState::Checked;
	return ECheckBoxState::Unchecked;
}


